package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.AnnualPriceDataDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.AnnualySharePriceData;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.SessionAttributes;

@Repository
@SessionAttributes
public class AnnualPriceDataDaoImpl extends
		AbstractDaoImpl<AnnualySharePriceData, Integer> implements
		AnnualPriceDataDao {

	protected AnnualPriceDataDaoImpl() {
		super(AnnualySharePriceData.class);
	}

	@Override
	public void saveAnnualySharePriceData(AnnualySharePriceData annualShareData) {
		saveOrUpdate(annualShareData);
	}

	@Override
	public List<AnnualySharePriceData> findAnnualySharePriceData(int companyId) {
		return findByCriteria(Restrictions.eq("companyId", companyId));
	}

	@Override
	public List<AnnualySharePriceData> searchAnnualySharePriceData(
			int companyId, Date fromYear, Date toYear, String orderBy,
			String orderByName) {
		Criteria criteria = getCurrentSession().createCriteria(
				AnnualySharePriceData.class);

		criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromYear != null) {
			criteria.add(Restrictions.ge("year", fromYear));
		}

		if (toYear != null) {
			criteria.add(Restrictions.le("year", toYear));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<AnnualySharePriceData> searchAnnualySharePriceData(
			int companyId, Date fromYear, Date toYear, String orderBy,
			String orderByName, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				AnnualySharePriceData.class);

		if (orderBy != null) {
			criteria.addOrder(Order.desc(getRecordOrder(orderBy)));
			if (orderByName != null) {
				criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
			}
		} else {
			criteria.addOrder(Order.desc("date"));

			criteria.createAlias("company", "cmp");
			criteria.addOrder(Order.asc("cmp.code"));
		}

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromYear != null) {
			criteria.add(Restrictions.ge("date", fromYear));
		}

		if (toYear != null) {
			criteria.add(Restrictions.le("date", toYear));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

	@Override
	@Transactional
	public void deleteAnnualySharePriceData(int companyId, Date fromDate,
			Date toDate) {
		String sql = "delete from AnnualySharePriceData where 1=1 ";
		if (companyId > Constants.ERROR) {
			sql += "and company_id = :companyId ";
		}
		if (fromDate != null) {
			sql += "and date >= :fromData ";
		}
		if (toDate != null) {
			sql += "and date <= :toDate ";
		}

		Query query = getCurrentSession().createQuery(sql);
		if (companyId > Constants.ERROR) {
			query.setInteger("companyId", companyId);
		}
		if (fromDate != null) {
			query.setParameter("fromDate", fromDate);
		}
		if (toDate != null) {
			query.setParameter("toDate", toDate);
		}

		query.executeUpdate();

	}

}
